﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace yule.CodeBuilder.Core.PDM
{
    public class pgConvert
    {
        internal static bool ConvertStringToBoolean(string p)
        {
            return false;
        }

        internal static System.Data.DbType ConvertStringToDbType(string sqltype)
        {
            System.Data.DbType dbType = System.Data.DbType.AnsiString;

            string type = sqltype;
            if (sqltype.IndexOf("(") > 0)
            {
                type = sqltype.Substring(0, sqltype.IndexOf("(")).Trim().ToLower();
            }


            switch (sqltype)
            {
                case "int":
                    dbType = System.Data.DbType.Int32;
                    break;
                case "varchar":
                    dbType = System.Data.DbType.AnsiString;
                    break;
                case "bit":
                    dbType = System.Data.DbType.Boolean;
                    break;
                case "datetime":
                    dbType = System.Data.DbType.DateTime;
                    break;
                case "decimal":
                    dbType = System.Data.DbType.Decimal;
                    break;
                case "float":
                    dbType = System.Data.DbType.Decimal;
                    break;
                case "image":
                    dbType = System.Data.DbType.Binary;
                    break;
                case "money":
                    dbType = System.Data.DbType.Decimal;
                    break;
                case "ntext":
                    dbType = System.Data.DbType.StringFixedLength;
                    break;
                case "nvarchar":
                    dbType = System.Data.DbType.AnsiString;
                    break;
                case "smalldatetime":
                    dbType = System.Data.DbType.DateTime;
                    break;
                case "smallint":
                    dbType = System.Data.DbType.Int16;
                    break;
                case "text":
                    dbType = System.Data.DbType.AnsiString;
                    break;
                case "bigint":
                    dbType = System.Data.DbType.Int64;
                    break;
                case "binary":
                    dbType = System.Data.DbType.Binary;
                    break;
                case "char":
                    dbType = System.Data.DbType.AnsiString;
                    break;
                case "nchar":
                    dbType = System.Data.DbType.AnsiString;
                    break;
                case "numeric":
                    dbType = System.Data.DbType.Decimal;
                    break;
                case "real":
                    
                    break;
                case "smallmoney":
                    
                    break;
                case "sql_variant":
                    
                    break;
                case "timestamp":
                    
                    break;
                case "tinyint":
                    
                    break;
                case "uniqueidentifier":
                    
                    break;
                case "varbinary":
                    dbType = System.Data.DbType.Binary;
                    break;
                case "xml":
                    
                    break;
            }
            return dbType;
        }

        internal static string ConvertStringToCSharpType(System.Data.DbType dbType)
        {

            switch (dbType)
            {
                case DbType.AnsiString:
                    return "string";
                case DbType.AnsiStringFixedLength:
                    return "string";
                case DbType.Binary:
                    return "byte[]";
                case DbType.Boolean:
                    return "bool";
                case DbType.Byte:
                    return "byte";
                case DbType.Currency:
                    return "decimal";
                case DbType.Date:
                    return "DateTime";
                case DbType.DateTime:
                    return "DateTime";
                case DbType.DateTime2:
                    return "DateTime";
                case DbType.DateTimeOffset:
                    return "DateTime";
                case DbType.Decimal:
                    return "decimal";
                case DbType.Double:
                    return "double";
                case DbType.Guid:
                    return "Guid";
                case DbType.Int16:
                    return "short";
                case DbType.Int32:
                    return "int";
                case DbType.Int64:
                    return "long";
                case DbType.Object:
                    return "object";
                case DbType.SByte:
                    return "sbyte";
                case DbType.Single:
                    return "float";
                case DbType.String:
                    return "string";
                case DbType.StringFixedLength:
                    return "string";
                case DbType.Time:
                    return "DateTime";
                case DbType.UInt16:
                    return "ushort";
                case DbType.UInt32:
                    return "uint";
                case DbType.UInt64:
                    return "ulong";
                case DbType.VarNumeric:
                    return "decimal";
                case DbType.Xml:
                    return "string";
                default:
                    return "object";
            }

        }

        public static string GetCSharpDefaultValue(string cSharpType)
        {
            switch (cSharpType)
            {
                case "int":
                    return "int.MinValue";
                case "double":
                    return "double.MinValue";
                case "DateTime":
                    return "DateTime.MinValue";
                default:
                    return "null";
            }
        }

    }
}
